package com.miyuan.view.home;

import android.Manifest;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.miyuan.R;
import com.miyuan.base.BaseActivity;

import java.util.List;

import butterknife.BindView;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 首页
 */

public class HomeActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener, EasyPermissions.PermissionCallbacks {
    @BindView(R.id.bottom_nav)
    BottomNavigationBar bottomNavigationBar;

    public int lastSelectPosition = 0;
    public MachineListFragment machineListFragment;
    public UserFragment userFragment;
    public ScanFragment scanFragment;
    private GoodsListFragment goodsListFragment;
    private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setLayout(R.layout.activity_home);
        super.onCreate(savedInstanceState);
        bottomNavigationBar
                .setTabSelectedListener(this)
                .setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
                .setActiveColor(R.color.blue_text2)
                .setInActiveColor(R.color.gray_text3)
                .setBarBackgroundColor(R.color.white);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.machine, "售货机"))
                .addItem(new BottomNavigationItem(R.mipmap.recruit_list, "补货清单"))
                .addItem(new BottomNavigationItem(R.mipmap.replenish, "补货"))
                .addItem(new BottomNavigationItem(R.mipmap.profile, "我的"))
                .setFirstSelectedPosition(lastSelectPosition)
                .initialise();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        machineListFragment = MachineListFragment.newInstance();
        transaction.replace(R.id.home_fragment_container, machineListFragment);
        transaction.commit();
    }

    @Override
    public void onTabSelected(int position) {
        lastSelectPosition = position;
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                if (machineListFragment == null) {
                    machineListFragment = MachineListFragment.newInstance();
                }
                transaction.replace(R.id.home_fragment_container, machineListFragment);
                break;
            case 1:
                if (goodsListFragment == null) {
                    goodsListFragment = GoodsListFragment.newInstance();
                }
                transaction.replace(R.id.home_fragment_container, goodsListFragment);
                break;
            case 2:
                if (scanFragment == null) {
                    scanFragment = ScanFragment.newInstance();
                }
                transaction.replace(R.id.home_fragment_container, scanFragment);
                break;
            case 3:
                if (userFragment == null) {
                    userFragment = UserFragment.newInstance();
                }
                transaction.replace(R.id.home_fragment_container, userFragment);
                break;
        }
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    @Override
    protected void onStart() {
        super.onStart();
        requestCodeQRCodePermissions();
    }

    @AfterPermissionGranted(REQUEST_CODE_QRCODE_PERMISSIONS)
    private void requestCodeQRCodePermissions() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, "扫描二维码需要打开相机和散光灯的权限", REQUEST_CODE_QRCODE_PERMISSIONS, perms);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }
}
